package
        com.chb.interceptor;

import com.chb.dao.SysOperLogDao;
import com.chb.model.entity.SysOperLog;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author：xIAO_ZHOU_TX
 * @date：Created in 2023/10/11 18:02
 * @description： 操作日志过滤器
 * @modified By：
 * @version:
 */
public class SysOperLogInterceptor implements HandlerInterceptor {
    @Autowired
    private SysOperLogDao sysOperLogDao;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        SysOperLog sysOperLog = new SysOperLog();
        //业务类型（businessType）
        String businessType = request.getRequestURI();
        if (businessType.equals("/role/insert")) {
            businessType = "添加角色";
        } else if (businessType.equals("/role/delete")) {
            businessType = "删除角色";
        } else if (businessType.equals("/role/update")) {
            businessType = "修改角色";
        } else {
            businessType = "查询角色";
        }
        sysOperLog.setBusinessType(businessType);
        System.out.println(businessType);
        //方法名称(method)
        String method = request.getRequestURI();
        sysOperLog.setMethod(method.substring(method.lastIndexOf("/") + 1));

        //请求方式[GET/POST] (requestMethod)
        String requestMethod = request.getMethod();
        sysOperLog.setRequestMethod(requestMethod);
        System.out.println(requestMethod);

        //操作人员(operName)
        Object operName = request.getSession().getAttribute("staff");
        Class<?> objClass = operName.getClass();
        // 获取属性名
        String fieldName = "myField";
        // 获取属性对象
        Field field = objClass.getDeclaredField("staffname");
        // 设置属性为可访问
        field.setAccessible(true);
        // 获取属性值
        Object fieldValue = field.get(operName);
        System.out.println(fieldValue);
        sysOperLog.setOperName(fieldValue.toString());

        //请求URL(operUrl)   /role/insert(添加)  /role/delete(删除)  /role/update(修改)
        String operUrl = request.getRequestURI();
        sysOperLog.setOperUrl(operUrl);
        System.out.println("operUrl" + operUrl);

        //主机地址(operip)
        String operip = request.getRemoteAddr();
        sysOperLog.setOperip(operip);
        System.out.println(operip);

        //请求参数(operParam)
        String operParam = request.getQueryString();
        sysOperLog.setOperParam(operParam);
        System.out.println(operParam);
        //返回参数(jsonResult)
        //操作状态(status)
        sysOperLog.setStatus(1);
        //操作时间(operTime)
        sysOperLog.setOperTime(new Date(System.currentTimeMillis()));
//        return HandlerInterceptor.super.preHandle(request, response, handler);

        System.out.println(sysOperLog);

        sysOperLogDao.insertRole(sysOperLog);
        return true;
//        return HandlerInterceptor.super.preHandle(request, response, sysOperLog);
    }

}